﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Entities;

namespace SimpleTaskManager.Domain.Concrete
{
    /// <summary>
    /// A repository containing the skills
    /// </summary>
    public class EFSkillRepository : ISkillRepository
    {
        private EFDbContext context = new EFDbContext();

        /// <summary>
        /// Gets the collection of the skills that can be used in the application
        /// </summary>
        public IQueryable<Skill> Skills
        {
            get { return context.Skills; }
        }

        /// <summary>
        /// Adds new skill to the repository
        /// </summary>
        /// <param name="skill">Skill to be added to the repository</param>
        public void Add(Skill skill)
        {
            context.Skills.Add(skill);
            context.SaveChanges();
        }

        /// <summary>
        /// Edits given skill
        /// </summary>
        /// <param name="skill">edited skill that will be saved to database</param>
        public void Edit(Skill skill)
        {
            context.Entry(skill).State = EntityState.Modified;
            context.SaveChanges();
        }

        /// <summary>
        /// Deletes skill identified by parameter id
        /// </summary>
        /// <param name="id">id of skill that should be deleted</param>
        public void Delete(int id)
        {
            var skill = context.Skills.Single(s => s.SkillID == id);
            context.Skills.Remove(skill);
            context.SaveChanges();
        }
    }
}
